﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;
using gdlmo.DNN.Modules.ImgNews.Business;

namespace gdlmo.DNN.Modules.ImgNews
{
	public abstract class ImgNews : PortalModuleBase, IActionable, IPortable, ISearchable 
	{
		protected System.Web.UI.HtmlControls.HtmlGenericControl divImgList;		

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion		

		#region Event Handlers

		private void Page_Load(object sender, System.EventArgs e)
		{
			try 
			{
				ImgNewsController objImgNews = new ImgNewsController();
				ArrayList list;
				if (!Page.IsPostBack) 
				{
					list = objImgNews.List(ModuleId);
					divImgList.InnerHtml = OutPutTable(list);
//					Label1.Attributes.Add("Marquee","<SCRIPT> var speed=30; demo2.innerHTML=demo1.innerHTML; function Marquee(){ if(demo2.offsetWidth-demo.scrollLeft<=0) demo.scrollLeft-=demo1.offsetWidth; else{ demo.scrollLeft++; } } var MyMar=setInterval(Marquee,speed); demo.onmouseover=function() {clearInterval(MyMar)} demo.onmouseout=function() {MyMar=setInterval(Marquee,speed)}</SCRIPT>");
				}
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		#endregion
		
		private string OutPutTable(ArrayList list)
		{
			const string TableHead =  "<TABLE id='LinkImgs' style='BORDER-COLLAPSE: collapse' cellSpacing=0 cellPadding=0 summary='LinkImage Design Table' border=0><TBODY><TR>";
			const string TableBot = "</TR></TBODY></TABLE>";
			
			const string TDHead = "<TD vAlign=top><TABLE cellSpacing=0 cellPadding=4 border=0><TD class='LinkImgCell' align=middle>";
			const string TDBot = "</TD></TABLE></TD>";			
			string temp;
			temp = TableHead;
//			DotNetNuke.UI.UserControls.UrlControl ctlIcon = new DotNetNuke.UI.UserControls.UrlControl();
			foreach(ImgNewsInfo objInfo in list)
			{
				temp += TDHead;
				temp += "<A title='" + objInfo.Title + "' href='" + objInfo.Links + "' target=_self>";
//				temp += "<IMG class=linkimage_image alt='" + objInfo.Title + "' src='" + GetMyURL(objInfo.ImgLink) +  "' border=0></Img>";
//				temp += "<IMG class=linkimage_image alt='" + objInfo.Title + "' src='" + Page.ResolveUrl("ImgNews/" + objInfo.ImgLink) +  "' border=0></Img>";
//				ctlIcon.Url = objInfo.ImgLink;
				
//				temp += "<IMG class=linkimage_image alt='" + objInfo.Title + "' src='http://localhost" +  DotNetNuke.Common.Globals.LinkClick(objInfo.ImgLink,TabId,ModuleId) +  "' border=0></Img>";

				temp += "<IMG class=linkimage_image alt='" + objInfo.Title + "' src='" +  PortalSettings.HomeDirectory + objInfo.ImgLink +  "' border=0></Img>";
				temp +="</a>";
				temp += TDBot;
			}
			temp += TableBot;
			return temp;
		}
		private string GetMyURL(string title)
		{
			string url = Page.Request.ApplicationPath + "/ImgNews/";
			return url + title;
		}	
		#region Optional Interfaces

		public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions 
		{
			get 
			{
				DotNetNuke.Entities.Modules.Actions.ModuleActionCollection Actions = new DotNetNuke.Entities.Modules.Actions.ModuleActionCollection();				
				Actions.Add(GetNextActionID(), Localization.GetString(DotNetNuke.Entities.Modules.Actions.ModuleActionType.AddContent, LocalResourceFile), DotNetNuke.Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), false, DotNetNuke.Security.SecurityAccessLevel.Edit, true, false);
				Actions.Add(GetNextActionID(),Localization.GetString("ImgNewsManage.Text", LocalResourceFile), DotNetNuke.Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl("ImgNewsManage"), false, DotNetNuke.Security.SecurityAccessLevel.Edit, true, false);
				return Actions;
			}
		}

		public string ExportModule(int ModuleID)
		{
			// included as a stub only so that the core knows this module Implements Entities.Modules.IPortable
			return null;
		}

		public void ImportModule(int ModuleID, string Content, string Version, int UserID)
		{
			// included as a stub only so that the core knows this module Implements Entities.Modules.IPortable
		}

		public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(ModuleInfo ModInfo)
		{
			// included as a stub only so that the core knows this module Implements Entities.Modules.ISearchable
			return null;
		}

		#endregion

	}
}